package com.foodsafe.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foodsafe.dao.AddivieinfoDao;
import com.foodsafe.model.Addivieinfo;
import com.foodsafe.util.PageInfo;

@Component("AddivieinfoDao")
public class AddivieinfoDaoImpl extends HibernateDaoImpl implements AddivieinfoDao {
	
	public List<Addivieinfo> getAllAddivieinfo(PageInfo pageInfo) throws Exception {
		String hql = "FROM Addivieinfo";
		if(pageInfo != null) {
			return findPageByQuery(hql, null, pageInfo);
		}
		return find(hql);
	}

	public List<Addivieinfo> getAllAddivieinfo(PageInfo pageInfo,
			String condition) throws Exception {
		String hql = "FROM Addivieinfo a where 1=1";
		if(condition.equals("time")) {
			hql += "ORDER BY a.infoPublishTime";
		} else if (condition.equals("click")) {
			hql += "ORDER BY a.click";
		}
		hql += " DESC";
		if(pageInfo != null) {
			return findPageByQuery(hql, null, pageInfo);
		}
		return find(hql);
	}

	public String getAddivieinfoContent(int id) throws Exception {
		String hql = "SELECT a.addivieInfoContent FROM Addivieinfo " +
				"a where a.addivieInfoId=" + id;
		List list = find(hql);
		return list.get(0).toString();
	}

	public void updateAddivieinfoClick(int id) throws Exception {
		Addivieinfo a = (Addivieinfo) get(Addivieinfo.class, id);
		a.setClick(a.getClick() + 1);
		update(a);
	}

	public void saveAddivieinfo(Addivieinfo addivieinfo) throws Exception {
		save(addivieinfo);
	}

}
